//
//  GPIndexHeaderView.m
//  gupiao
//
//  Created by mac on 2019/8/14.
//  Copyright © 2019 mac. All rights reserved.
//

#import "GPIndexHeaderView.h"
#import "大股票-Swift.h"

@interface GPIndexHeaderView ()<FSPagerViewDelegate,FSPagerViewDataSource>
@property (nonatomic,strong) FSPagerView *pagerView;
@property (nonatomic,strong) FSPageControl *pageControl;
@end

@implementation GPIndexHeaderView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.bannerHeight.constant = 240*FC_AUTO;
    [self initPagerView];
    [self.bannerView addSubview:self.pageControl];
}

-(void)setSourceArr:(NSArray *)sourceArr{
    _sourceArr = sourceArr;
    self.pageControl.numberOfPages = sourceArr.count;
    [self.pagerView reloadData];
}

- (IBAction)addAction:(id)sender {
    if (self.addBlock) {
        self.addBlock(sender);
    }
}

- (IBAction)changeAction:(id)sender {
    if (self.changeBlock) {
        self.changeBlock();
    }
}

- (IBAction)applyAction:(id)sender {
    if (self.applyBlock) {
        self.applyBlock(sender);
    }
}

- (NSInteger)numberOfItemsInPagerView:(FSPagerView * _Nonnull)pagerView {
    return self.sourceArr.count;
}

- (FSPagerViewCell *)pagerView:(FSPagerView *)pagerView cellForItemAtIndex:(NSInteger)index
{
    
    FSPagerViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"indexBannerCell" atIndex:index];
//    cell.imageView.layer.cornerRadius = 2;
    cell.imageView.image = [UIImage imageNamed:self.sourceArr[index]];
    cell.imageView.clipsToBounds = YES;
    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    //    cell.contentView.layer.shadowColor = UIColor.black.cgColor
    cell.contentView.layer.shadowColor = [UIColor clearColor].CGColor;
    //    cell.contentView.layer.shadowOpacity = 0.75
    //    cell.contentView.layer.shadowOffset = .zero

    return cell;
}

- (void)pagerView:(FSPagerView *)pagerView didSelectItemAtIndex:(NSInteger)index
{
//    [pagerView deselectItemAtIndex:index animated:NO];
    [pagerView scrollToItemAtIndex:index animated:YES];
}

-(BOOL)pagerView:(FSPagerView *)pagerView shouldHighlightItemAtIndex:(NSInteger)index{
    return NO;
}

- (void)pagerViewWillEndDragging:(FSPagerView *)pagerView targetIndex:(NSInteger)targetIndex
{
    self.pageControl.currentPage = targetIndex;
}

- (void)pagerViewDidEndScrollAnimation:(FSPagerView *)pagerView
{
    self.pageControl.currentPage = pagerView.currentIndex;
}

-(void)initPagerView{
    
    _pagerView = [[FSPagerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240*FC_AUTO)];
    [self.bannerView addSubview:_pagerView];
    _pagerView.dataSource = self;
    _pagerView.delegate = self;
    [_pagerView registerClass:[FSPagerViewCell class] forCellWithReuseIdentifier:@"indexBannerCell"];
    //        _pagerView.transformer = [[FSPagerViewTransformer alloc] initWithType:FSPagerViewTransformerTypeLinear];
    _pagerView.automaticSlidingInterval = 3.0;
    _pagerView.isInfinite = YES;
    _pagerView.itemSize = CGSizeMake(SCREEN_WIDTH, 240*FC_AUTO);
    _pagerView.interitemSpacing = 0;
}

-(FSPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[FSPageControl alloc] initWithFrame:CGRectMake(0, (240*FC_AUTO)-20, SCREEN_WIDTH, 20)];
        [_pageControl setFillColor:UIColorFromRGB(0xDCDCDC) forState:UIControlStateNormal];
    }
    return _pageControl;
}

@end
